package com.fjh.controller.sys;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import com.fjh.common.Result;
import com.fjh.domain.SysUser;
import com.fjh.dto.SysUserDto;
import com.fjh.service.UserService;
import com.fjh.vo.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@RestController
@RequestMapping("/sysuser")
public class SysUserController {

    @Autowired
    private UserService userService;

    /**
     * 用户登录
     * @param username
     * @param password
     */
    @PostMapping("/login.do")
    public Result doLogin(String username, String password,String code){
        //校验验证码
        validateCode(code);
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        Subject subject = SecurityUtils.getSubject();
        subject.login(token);
        System.out.println("登陆成功");
        return Result.ok();
    }

    /**
     * 用户的登出
     * @return
     */
    @GetMapping("/logout.do")
    public Result logout(){
        //shiro实现登出
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return Result.ok();
    }
    /**
     * 生成验证码
     * @param resp
     * @param session
     * @throws IOException
     */
    @GetMapping("/codeImg.do")
    public void createCodeImg(HttpServletResponse resp, HttpSession session) throws IOException {
        CircleCaptcha circleCaptcha = CaptchaUtil.createCircleCaptcha(130,48,1,0);
        session.setAttribute("CODE_IMG", circleCaptcha.getCode());
        circleCaptcha.write(resp.getOutputStream());
    }

    /**
     * 校验验证码
     * @param code
     */
    private void validateCode(String code) {
        //1.获取session里面的验证码
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpSession session = requestAttributes.getRequest().getSession();
        Object codeImg = session.getAttribute("CODE_IMG");
        if (codeImg == null){
            throw new IllegalArgumentException("请获取验证码后重试");
        }
        if (!codeImg.toString().equalsIgnoreCase(code)){
            throw new IllegalArgumentException("验证码输入错误");
        }
    }

    //////////////////////////////////////////////用户的增删改查///////////////////////////////////////////
    /**
     *   接收前端的参数,我们以后使用DTO 对象来接收
     *   Data Transport Object
     *   接收前端的参数传递给Service
     *  我们给前端响应数据时,我们响应数据名称为: Vo
     *  View Object
     *
     *  domain = > 模型类 == > POJO 对象
     *
     *  (前端->后端) (后端==>数据库) (后端==>前端)
     *  DTO = >     POJO = >       VO
     *
     */
    /**
     * 新增用户
     * @param dto
     * @return
     */
    @PostMapping("/add.do")
    public Result addUser(@RequestBody  SysUserDto dto){
        boolean isOk = userService.addUser(dto);
        return isOk? Result.ok():Result.fail();
    }

    /**
     * 更改用户状态
     * @param dto
     * @return
     */
    @PostMapping("/updateUserStatus.do")
    public Result updateUserStatus(SysUserDto dto){
        boolean isOk = userService.updateUser(dto);
        return isOk ? Result.ok() : Result.fail();
    }

    /**
     * 删除用户
     * @param ids
     * @return
     */
    @PostMapping("/delete.do/{ids}")
    public Result deleteUser(@PathVariable("ids") Integer[] ids){
        boolean isDelete = userService.batchDelete(ids);
        return isDelete? Result.ok() : Result.fail();
    }

    /**
     * 修改用户
     * @param dto
     * @return
     */
    @PostMapping("/update.do")
    public Result updateUser(@RequestBody  SysUserDto dto){
        boolean isOk = userService.updateUser(dto);
        return isOk? Result.ok():Result.fail();
    }

    /**
     * 分页查找
     * @param dto
     * @return
     */
    @GetMapping("/findByPage.do")
    public Result findByPage(SysUserDto dto){
        PageInfo<SysUser> pageData = userService.findByPage(dto);
        return Result.ok(pageData);
    }
}
